﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using CSharp.Libs.Koc;
using System.Runtime.InteropServices;
using System.Collections.ObjectModel;
using CSharp.Libs.Koc.Models;

namespace CSharp.Libs.WPF.UserControls
{
    public class KocMarchControlViewModel : ViewModelBase, IDisposable
    {
        #region declarations
        private Dispatcher _DISP = null;
        private Kernel _KRNL = null;
        private KocKernel2ViewModel _KK2VM = null;
        private PostHandler _PH = null;
        private MarchHandler _MH = null;

        private MarchHandlerItem _mhiSelectedMarch = new MarchHandlerItem();

        private ObservableCollection<MarchTypeItem> _ocMarchTypes = new ObservableCollection<MarchTypeItem>();
        private MarchTypeItem _mtiSelectedMarch = new MarchTypeItem(MarchTypes.None);
        #endregion



        #region constructor / finalizer
        public KocMarchControlViewModel(
            Dispatcher DISP,
            Kernel KRNL,
            KocKernel2ViewModel KK2VM)
        {
            _DISP = DISP;
            _KRNL = KRNL;
            _KK2VM = KK2VM;
            _PH = new PostHandler(_KRNL);

            _SetMarchHandlerItem(new MarchHandlerItem());
            _SetSelectedMarchType();

            _createMarchTypes();
        }

        #region IDisposable members
        private bool _fDisposed = false; // to detect redundant calls
        protected virtual void Dispose(
            bool disposing)
        {
            if (!_fDisposed)
            {
                if (disposing)
                {
                    //cleanup managed handles
                    if (_DISP != null) { _DISP = null; } 
                    if (_KRNL != null) { _KRNL = null; }
                    if (_KK2VM != null) { _KK2VM = null; }
                    if (_PH != null) { _PH.Dispose(); _PH = null; }
                    if (_MH != null) { _MH.Dispose(); _MH = null; }
                    if (_mhiSelectedMarch != null) { _mhiSelectedMarch = null; }
                    if (_ocMarchTypes != null) { _ocMarchTypes = null; }
                }

                _fDisposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
        }
        #endregion
        #endregion



        #region event handling
        void _mhiSelectedMarch_PropertyChanged(object sender, PropertyChangedEventArgs pcea)
        {
            if (pcea.PropertyName == "MarchType")
            {
                _SetSelectedMarchType();
            }
            else
            {
                OnPropertyChanged("MHI");
            }
        }
        #endregion



        #region private functions
        #region initialisations
        private void _createMarchTypes()
        {
            _ocMarchTypes.Add(new MarchTypeItem(MarchTypes.None));
            _ocMarchTypes.Add(new MarchTypeItem(MarchTypes.Transport));
            _ocMarchTypes.Add(new MarchTypeItem(MarchTypes.Reinforce));
            _ocMarchTypes.Add(new MarchTypeItem(MarchTypes.Scout));
            _ocMarchTypes.Add(new MarchTypeItem(MarchTypes.Attack));
            _ocMarchTypes.Add(new MarchTypeItem(MarchTypes.Reassign));
        }
        #endregion

        #region MarchHandlerItem handling
        private void _SetMarchHandlerItem(
            MarchHandlerItem mhi)
        {
            _mhiSelectedMarch.PropertyChanged -= _mhiSelectedMarch_PropertyChanged;
            _mhiSelectedMarch = mhi;
            _mhiSelectedMarch.PropertyChanged += new MarchHandlerItem.PropertyChangedEventHandler(_mhiSelectedMarch_PropertyChanged);

            _SetSelectedMarchType();
        }

        private void _SetSelectedMarchType()
        {
            foreach (MarchTypeItem mti in _ocMarchTypes)
            {
                if (mti.MarchType == _mhiSelectedMarch.MarchType)
                {
                    _mtiSelectedMarch = mti;
                    OnPropertyChanged("SelectedMarch");
                    break;
                }
            }
        }
        #endregion
        #endregion



        #region interface
        #region properties
        public ObservableCollection<MarchTypeItem> MarchTypesList { get { return _ocMarchTypes; } }

        public MarchHandlerItem MHI { get { return _mhiSelectedMarch; } }

        public MarchTypeItem SelectedMarch
        {
            get { return _mtiSelectedMarch; }
            set
            {
                _mtiSelectedMarch = value;
                _mhiSelectedMarch.MarchType = _mtiSelectedMarch.MarchType;
            }
        }
        #endregion

        #region methods
        public void SetMarchHandlerItem(
            MarchHandlerItem mhi)
        {
            _SetMarchHandlerItem(mhi);

            //ToDo: So geht das mit dem PHP Command. :)
            //PhpCommand pcMarch = PhpCommandGenerator.GetPhpCommand(_KRNL.EC, PhpCommands.march, _KK2VM.ServerId, _KK2VM.SelectedUser.FbUid);
        }

        public void ClearMarchHandlerItem()
        {
            _SetMarchHandlerItem(new MarchHandlerItem());
        }
        #endregion
        #endregion
    }
}